﻿using Absky.Util;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using System;

namespace Absky.Web.Controllers
{
    /// <summary>
    /// 错误控制器
    /// </summary>
    public class ErrorController : Controller
    {
        /// <summary>
        /// 全局未捕获的异常错误处理
        /// </summary>
        /// <returns></returns>
        [HttpGet("/Error")]
        public IActionResult Index()
        {
            //获取异常的详细信息
            IExceptionHandlerPathFeature iExceptionHandlerFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
            if (iExceptionHandlerFeature != null)
            {
                //string path = iExceptionHandlerFeature.Path;//发生异常的路由
                Exception exception = iExceptionHandlerFeature.Error;
                LogHelper.Error(exception);
                string errorMessage = exception.GetOriginalException().Message;
                if (HttpContext.Request.IsAjaxRequest())
                {
                    //ajax请求返回json错误信息
                    Result obj = new Result
                    {
                        Message = string.IsNullOrEmpty(errorMessage) ? "抱歉，系统出错了，请联系管理员！" : errorMessage
                    };
                    return Json(obj);
                }
                else
                {
                    //页面出错返回错误页面
                    return View("Error", iExceptionHandlerFeature);
                }
            }
            return View();//默认错误页，没有错误
        }
        /// <summary>
        /// 全局404处理
        /// </summary>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        [HttpGet("/Error/NotFound/{statusCode}")]
        public IActionResult NotFound(int statusCode)
        {
            var iStatusCodeReExecuteFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
            if(iStatusCodeReExecuteFeature != null)
            {
                if (HttpContext.Request.IsAjaxRequest())
                {
                    //ajax请求返回json错误信息
                    Result obj = new Result
                    {
                        Message = statusCode + " not found."
                    };
                    return Json(obj);
                }
            }
            return View("Path404");
        }
        /// <summary>
        /// 未经授权
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult NoPermission()
        {
            return View();
        }
        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Message(string message)
        {
            ViewBag.Message = message;
            return View();
        }
        /// <summary>
        /// 错误页面（错误路径404）
        /// </summary>
        /// <returns></returns>
        public IActionResult Path404()
        {
            return View();
        }
        /// <summary>
        /// 错误页面（升级浏览器软件）
        /// </summary>
        /// <returns></returns>
        public IActionResult ErrorBrowser()
        {
            return View();
        }
    }
}
